package com.csc.mtlbatch.vch.cls;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.csc.dataaccess.dao.LjdeexttrtDao;
import com.csc.dataaccess.entity.Ljdeexttrt;
import com.csc.mtlbatch.vch.batchprograms.Bt086;

public class Ct086 {
	
	static Properties prop = new Properties();
	static InputStream input = null;
	static String fileout = "";
	
	private static final Logger LOGGER = LoggerFactory.getLogger(Ct086.class);
	
	private static void init() throws IOException{
		try {
			input = new FileInputStream("mtlbatchconfig.properties");
			prop.load(input);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    	
	}
	
	public Boolean writing() {
		
		String strfileName  = "";
		String strDateStamp  = "";
		String strTimeStamp  = "";
		Boolean retWrite = false;
		
	    try {
	    	LOGGER.info("init Ct086");
	    	init();
	    	fileout = prop.getProperty("FILEOUTPATH");
	    	
	    	strDateStamp = this.getDateStampFormat();	
	    	strTimeStamp = this.getTimeStampFormat();
	    	    	
	    	strfileName = this.getfileName(strDateStamp,strTimeStamp);
	    	
	    	List<Ljdeexttrt> ljdList = null;
	    	ljdList = this.getAllLjdeexttrtValues();
	    	
	    	File statText = new File(fileout + strfileName);
	        FileOutputStream is = new FileOutputStream(statText);
	        OutputStreamWriter osw = new OutputStreamWriter(is);    
	        BufferedWriter w = new BufferedWriter(osw);
	    	
	        w.write(this.getLjdHeaderFormat(strfileName, strDateStamp));
	        
	    	for (Ljdeexttrt next : ljdList) {
	    		//System.out.println("next Ljdeexttrt: " + this.getLjdDetailFormat(next));
	    		w.newLine();
	    		w.write(this.getLjdDetailFormat(next));
	    	}
	    	
	    	w.newLine();
	    	w.write(this.getLjdFooterFormat(ljdList));
	    	LOGGER.info("File " + strfileName + " Generated.");
	        w.close();
	        retWrite = true;
	    } catch (IOException e) {
	        //System.err.println("Problem writing to the file statsTest.txt");
	        LOGGER.error("Problem writing to the file.");
	        retWrite = false;
	    }
	    
	    return retWrite;
	}
	
	private List<Ljdeexttrt> getAllLjdeexttrtValues() {

		LjdeexttrtDao ljdeexttrtDao = new LjdeexttrtDao();

		List<Ljdeexttrt> LjdeList = ljdeexttrtDao.getAllLjdeexttrt();

		return LjdeList;
	}
	
	private String getfileName(String dateStamp, String timeStamp){
		return "IL_JDE_ORDUTR_" + dateStamp + timeStamp + ".dat";
	}
	
	private String getDateStampFormat(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date now = new Date();
	    String strDate = sdf.format(now);
		return strDate;
	}
	
	private String getTimeStampFormat(){
		SimpleDateFormat sdf = new SimpleDateFormat("HHmmssSSS");
		Date now = new Date();
	    String strTime = sdf.format(now);
		return strTime;
	}
	
	private String getLjdHeaderFormat(String fileName, String dateStamp){
		return "HEADER" + "," + fileName + "," + dateStamp;
	}
	
	private String getLjdDetailFormat(Ljdeexttrt data){
		String retData = "";
		
		retData = data.getTrcoco() + ","
				+ data.getTrtena() + ","
				+ data.getTrtenu() + ","
				+ data.getTrtrsd() + ","
				+ data.getTrtrst() + ","
				+ data.getTrtrse() + ","
				+ data.getTrdico() + ","
				+ data.getTrbanu() + ","
				+ data.getTrtrna() + ","
				+ data.getTrinit() + ","
				+ data.getTrreco() + ","
				+ data.getTrmess();
		
		return retData;
	}
	
	private String getLjdFooterFormat(List<Ljdeexttrt> dataList){
		int rowSize = dataList.size();
		rowSize = rowSize + 1;
		return "FOOTER" + "," + rowSize;
	}

	public static void main(String[]args) {
		Ct086 write = new Ct086();
	    write.writing();
		//System.out.println(write.getTimeStampFormat());
	}
}
